//
//  NSString+Tools.m
//  yayi
//
//  Created by hxp on 16/3/13.
//  Copyright © 2016年 hxp. All rights reserved.
//

#import "NSString+Tools.h"
#import <CommonCrypto/CommonDigest.h>
@implementation NSString (Tools)

+(NSMutableAttributedString *)content:(NSString *)title name:(NSString *)name
{
    NSString *string = [NSString stringWithFormat:@"%@：%@",title,name];
    NSMutableAttributedString *maString = [[NSMutableAttributedString alloc] initWithString:string];
    NSRange rang =  [string  rangeOfString:@"："];
    [maString  addAttribute:NSForegroundColorAttributeName  value:[UIColor lightGrayColor] range:NSMakeRange(0, rang.location + 1)];
    [maString addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(rang.location + 1, maString.length - rang.location -1)];
    return maString;
}

+(NSString *)payState:(NSString *)state
{
    if ([state isEqualToString:@"0"]) {
        return @"未生效";
    }else if ([state isEqualToString:@"1"]){
        return @"未付款";
    }else if ([state isEqualToString:@"2"]){
        return @"待评价";
    }else if ([state isEqualToString:@"3"]){
        return @"完成";
    }else{
        return @"取消";
    }
}
+(NSString *)dateString:(NSString *)time
{
    int t = [time doubleValue] / 1000;
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:t];
    NSDateFormatter *formater = [[NSDateFormatter alloc] init];
    formater.dateFormat = @"YYYY-MM-dd";
    NSString *dateStr = [formater  stringFromDate:date];
    return dateStr;
}

+(NSString *)timeString:(NSString *)time
{
    int t = [time doubleValue] / 1000;
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:t];
    NSDateFormatter *formater = [[NSDateFormatter alloc] init];
    formater.dateFormat = @"HH:mm";
    NSString *dateStr = [formater  stringFromDate:date];
    return dateStr;
}


+(NSString *)ageStr:(NSString *)birthday
{
    if (birthday == nil) {
        return @" ";
    }
    NSString *birthdateString = [self dateString:birthday];
    int  birth = [[birthdateString  substringToIndex:4] intValue];
    NSDate *date = [NSDate new];
    NSDateFormatter *formater = [NSDateFormatter new];
    formater.dateFormat = @"YYYY";
    NSString *dateStr = [formater  stringFromDate:date];
    int curDate = [dateStr intValue];
    return [NSString stringWithFormat:@"%d",(curDate - birth)];
    
}


- (CGSize)sizewithFont:(UIFont *)font maxSize:(CGSize)maxSize
{
    NSDictionary *attrs = @{NSFontAttributeName : font};
    
    return [self boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;
}

- (CGSize)sizeWithFont:(UIFont *)font
{
    return [self sizewithFont:font maxSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX)];
}

NSString * MD5Hash(NSString *aString) {
    const char *cStr = [aString UTF8String];
    unsigned char result[16];
    CC_MD5( cStr, (CC_LONG)strlen(cStr), result );
    return [NSString stringWithFormat:@"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
            result[0], result[1], result[2], result[3],
            result[4], result[5], result[6], result[7],
            result[8], result[9], result[10], result[11],
            result[12], result[13], result[14], result[15]];
}

+(NSMutableAttributedString *)titleStr:(NSString *)title content:(NSString *)content font:(UIFont *)font
{
    NSMutableAttributedString *ABstr = nil;
    if (content.length == 0) {
       ABstr = [[NSMutableAttributedString alloc] initWithString:title];
      [ABstr addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:122/255.0 green:120/255.0 blue:120/255.0 alpha:1] range:NSMakeRange(0, 4)];
    }else{
        NSString *str = [NSString stringWithFormat:@"%@ %@",title,content];
        ABstr = [[NSMutableAttributedString alloc] initWithString:str];
        [ABstr addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:122/255.0 green:120/255.0 blue:120/255.0 alpha:1] range:NSMakeRange(0, 4)];
        [ABstr addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:38/255.0 green:37/255.0 blue:37/255.0 alpha:1] range:NSMakeRange(5, ABstr.length - 6)];
    }
    
    [ABstr addAttribute:NSFontAttributeName value:font  range:NSMakeRange(0, ABstr.length)];
    return ABstr;
    
}


+ (NSString *)valiMobile:(NSString *)mobile{
    if (mobile.length < 11)
    {
        return @"手机号长度只能是11位";
    }else{
        /**
         * 移动号段正则表达式
         */
        NSString *CM_NUM = @"^((13[4-9])|(147)|(15[0-2,7-9])|(178)|(18[2-4,7-8]))\\d{8}|(1705)\\d{7}$";
        /**
         * 联通号段正则表达式
         */
        NSString *CU_NUM = @"^((13[0-2])|(145)|(15[5-6])|(176)|(18[5,6]))\\d{8}|(1709)\\d{7}$";
        /**
         * 电信号段正则表达式
         */
        NSString *CT_NUM = @"^((133)|(153)|(177)|(18[0,1,9]))\\d{8}$";
        NSPredicate *pred1 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM_NUM];
        BOOL isMatch1 = [pred1 evaluateWithObject:mobile];
        NSPredicate *pred2 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU_NUM];
        BOOL isMatch2 = [pred2 evaluateWithObject:mobile];
        NSPredicate *pred3 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT_NUM];
        BOOL isMatch3 = [pred3 evaluateWithObject:mobile];
        
        if (isMatch1 || isMatch2 || isMatch3) {
            return nil;
        }else{
            return @"请输入正确的手机号码";
        }
    }
    return nil;
}


//汉字的拼音
- (NSString *)pinyin
{
    NSMutableString *str = [self mutableCopy];
    CFStringTransform(( CFMutableStringRef)str, NULL, kCFStringTransformMandarinLatin, NO);
    CFStringTransform((CFMutableStringRef)str, NULL, kCFStringTransformStripDiacritics, NO);
    
    return [str stringByReplacingOccurrencesOfString:@" " withString:@""];
}


@end
